package com.icity.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.icity.entity.ad.AdSummary;
import com.icity.entity.ad.Banner;
import com.icity.entity.system.TableSupport;
import com.icity.mapper.AdMapper;
import com.icity.service.AdService;
import com.icity.util.DateUtils;

@Service
public class AdServiceImpl implements AdService {

	@Autowired
	private AdMapper adMapper;

	@Override
	public TableSupport<?> getBannerTable(Map<Object, Object> map) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		TableSupport<Banner> listTable = new TableSupport<Banner>();
		List<Banner> listBanner = adMapper.getBannerList(map);
		for (Banner banner : listBanner) {
			if (banner.getEnd_time() != null) {
				banner.setStart_time_string(sdf.format(banner.getEnd_time()));
			}
			if (banner.getStart_time() != null) {
				banner.setEnd_time_string(sdf.format(banner.getStart_time()));
			}
		}
		int count = adMapper.getBannerCount(map);
		listTable.setCode(0);
		listTable.setMsg(null);
		listTable.setCount(count);
		listTable.setData(listBanner);
		return listTable;
	}

	@Override
	public Map<Object, Object> getWeekDay(Map<Object, Object> mapDate, int days) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		List<String> list = new ArrayList<String>();
		if (mapDate.containsKey("nowDate")) {
			String date = mapDate.get("nowDate").toString();
			for (int i = days - 1; i >= 0; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(date, i));
			}
		} else {
			days = (int) DateUtils.getDaySub(mapDate.get("startDate").toString(), mapDate.get("endDate").toString());
			for (int i = days; i >= 0; i--) {
				list.add(DateUtils.getSpecifiedDayBefore(mapDate.get("endDate").toString(), i));
			}
		}
		map.put("type", "category");
		map.put("boundaryGap", false);
		map.put("data", list);
		return map;
	}

	@Override
	public Map<Object, Object> getGrid() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("left", "3%");
		map.put("right", "4%");
		map.put("bottom", "3%");
		map.put("containLabel", true);
		return map;
	}

	@Override
	public Map<Object, List<String>> getLegend() {
		return null;
	}

	@Override
	public Map<Object, String> getTooltip() {
		Map<Object, String> map = new HashMap<Object, String>();
		map.put("trigger", "axis");
		return map;
	}

	@Override
	public Map<Object, Object> getYaxis() {
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("type", "value");
		return map;
	}

	@Override
	public List<Map<Object, Object>> getSeries(Map<Object, Object> map) {
		List<Map<Object, Object>> listSeries = new ArrayList<Map<Object, Object>>();
		Map<Object, Object> maps = new HashMap<Object, Object>();
		List<AdSummary> listAdSummary = adMapper.getAdSummary(map);
		List<Integer> listClick = new ArrayList<Integer>();
		for (AdSummary adSummary : listAdSummary) {
			listClick.add(adSummary.getValue());
		}
		maps.put("name", "summary");
		maps.put("type", "line");
		maps.put("stack", "总量");
		maps.put("data", listClick);
		listSeries.add(maps);
		return listSeries;
	}

}
